const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const memberLevelController = require('../controllers/memberLevel.controller');

const router = new Router({ prefix: '/api/v1/member-levels' });

/**
 * @swagger
 * /api/v1/member-levels:
 *   post:
 *     tags: [会员等级]
 *     summary: 创建会员等级
 *     description: 创建新的会员等级配置
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - level
 *               - pointsRequired
 *             properties:
 *               name:
 *                 type: string
 *                 description: 等级名称
 *                 example: "黄金会员"
 *               level:
 *                 type: integer
 *                 description: 等级数值（越大等级越高）
 *                 example: 3
 *               pointsRequired:
 *                 type: integer
 *                 description: 达到该等级所需积分
 *                 example: 1000
 *               benefits:
 *                 type: object
 *                 description: 等级权益配置
 *                 example: {
 *                   "freeShipping": true,
 *                   "birthdayBonus": 500,
 *                   "exclusiveService": true
 *                 }
 *               description:
 *                 type: string
 *                 description: 等级说明
 *                 example: "尊贵的黄金会员享有专属特权服务"
 *               icon:
 *                 type: string
 *                 description: 等级图标URL
 *                 example: "https://example.com/icons/gold.png"
 *               color:
 *                 type: string
 *                 description: 等级主题色
 *                 example: "#FFD700"
 *               discountRate:
 *                 type: number
 *                 format: float
 *                 description: 会员折扣率
 *                 example: 0.85
 *               monthlyPoints:
 *                 type: integer
 *                 description: 每月赠送积分
 *                 example: 100
 *               specialPrivileges:
 *                 type: object
 *                 description: 特殊权限配置
 *                 example: {
 *                   "priorityService": true,
 *                   "exclusiveEvents": true
 *                 }
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "level_001"
 *                 message:
 *                   type: string
 *                   example: "会员等级创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission('member_level:create'), memberLevelController.createMemberLevel);

/**
 * @swagger
 * /api/v1/member-levels:
 *   get:
 *     tags: [会员等级]
 *     summary: 获取会员等级列表
 *     description: 获取所有会员等级配置（支持状态筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive]
 *         description: 等级状态筛选（活跃/停用）
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 levels:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "level_001"
 *                       name:
 *                         type: string
 *                         example: "黄金会员"
 *                       level:
 *                         type: integer
 *                         example: 3
 *                       pointsRequired:
 *                         type: integer
 *                         example: 1000
 *                       status:
 *                         type: string
 *                         example: "active"
 *                       memberCount:
 *                         type: integer
 *                         example: 150
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('member_level:read'), memberLevelController.getMemberLevels);

/**
 * @swagger
 * /api/v1/member-levels/{id}:
 *   get:
 *     tags: [会员等级]
 *     summary: 获取会员等级详情
 *     description: 获取指定会员等级的详细配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 会员等级ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "level_001"
 *                 name:
 *                   type: string
 *                   example: "黄金会员"
 *                 level:
 *                   type: integer
 *                   example: 3
 *                 pointsRequired:
 *                   type: integer
 *                   example: 1000
 *                 benefits:
 *                   type: object
 *                   example: {
 *                     "freeShipping": true,
 *                     "birthdayBonus": 500
 *                   }
 *                 description:
 *                   type: string
 *                   example: "尊贵的黄金会员享有专属特权服务"
 *                 icon:
 *                   type: string
 *                   example: "https://example.com/icons/gold.png"
 *                 color:
 *                   type: string
 *                   example: "#FFD700"
 *                 discountRate:
 *                   type: number
 *                   example: 0.85
 *                 monthlyPoints:
 *                   type: integer
 *                   example: 100
 *                 specialPrivileges:
 *                   type: object
 *                   example: {
 *                     "priorityService": true,
 *                     "exclusiveEvents": true
 *                   }
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 memberCount:
 *                   type: integer
 *                   example: 150
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 updatedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 会员等级不存在
 */
router.get('/:id', checkAuth, checkPermission('member_level:read'), memberLevelController.getMemberLevel);

/**
 * @swagger
 * /api/v1/member-levels/{id}:
 *   put:
 *     tags: [会员等级]
 *     summary: 更新会员等级
 *     description: 更新指定会员等级的配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 会员等级ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 等级名称
 *               pointsRequired:
 *                 type: integer
 *                 description: 达到该等级所需积分
 *               benefits:
 *                 type: object
 *                 description: 等级权益配置
 *               description:
 *                 type: string
 *                 description: 等级说明
 *               icon:
 *                 type: string
 *                 description: 等级图标URL
 *               color:
 *                 type: string
 *                 description: 等级主题色
 *               discountRate:
 *                 type: number
 *                 description: 会员折扣率
 *               monthlyPoints:
 *                 type: integer
 *                 description: 每月赠送积分
 *               specialPrivileges:
 *                 type: object
 *                 description: 特殊权限配置
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "会员等级更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 会员等级不存在
 */
router.put('/:id', checkAuth, checkPermission('member_level:update'), memberLevelController.updateMemberLevel);

/**
 * @swagger
 * /api/v1/member-levels/{id}:
 *   delete:
 *     tags: [会员等级]
 *     summary: 删除会员等级
 *     description: 删除指定的会员等级配置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 会员等级ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "会员等级删除成功"
 *       400:
 *         description: 无法删除（等级正在使用中）
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 会员等级不存在
 */
router.delete('/:id', checkAuth, checkPermission('member_level:delete'), memberLevelController.deleteMemberLevel);

/**
 * @swagger
 * /api/v1/member-levels/{id}/status:
 *   patch:
 *     tags: [会员等级]
 *     summary: 更新会员等级状态
 *     description: 更新指定会员等级的状态（启用/停用）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 会员等级ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *                 description: 新的状态（活跃/停用）
 *     responses:
 *       200:
 *         description: 状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "会员等级状态更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 会员等级不存在
 */
router.patch('/:id/status', checkAuth, checkPermission('member_level:update'), memberLevelController.updateMemberLevelStatus);

/**
 * @swagger
 * /api/v1/member-levels/{id}/users:
 *   get:
 *     tags: [会员等级]
 *     summary: 获取等级会员列表
 *     description: 获取指定等级下的所有会员列表
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 会员等级ID
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 150
 *                 pages:
 *                   type: integer
 *                   example: 15
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 users:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "user_001"
 *                       name:
 *                         type: string
 *                         example: "张三"
 *                       points:
 *                         type: integer
 *                         example: 1200
 *                       joinTime:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: ���认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 会员等级不存在
 */
router.get('/:id/users', checkAuth, checkPermission('member_level:read'), memberLevelController.getMemberLevelUsers);

/**
 * @swagger
 * /api/v1/member-levels/users/{userId}/check-upgrade:
 *   post:
 *     tags: [会员等级]
 *     summary: 检查会员升级
 *     description: 检查并处理指定会员的等级升级资格
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 检查完成
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 eligible:
 *                   type: boolean
 *                   example: true
 *                 currentLevel:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                       example: "level_001"
 *                     name:
 *                       type: string
 *                       example: "黄金会员"
 *                     level:
 *                       type: integer
 *                       example: 3
 *                 nextLevel:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                       example: "level_002"
 *                     name:
 *                       type: string
 *                       example: "铂金会员"
 *                     level:
 *                       type: integer
 *                       example: 4
 *                     pointsRequired:
 *                       type: integer
 *                       example: 2000
 *                 pointsToUpgrade:
 *                   type: integer
 *                   example: 800
 *                 message:
 *                   type: string
 *                   example: "恭喜您已满足升级条件"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 用户不存在
 */
router.post('/users/:userId/check-upgrade', checkAuth, checkPermission('member_level:update'), memberLevelController.checkUserLevelUpgrade);

module.exports = router; 